/*
 * Copyright (c) 2020-2022. the original authors and DEPSEA.ORG
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package org.depsea.log.config;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import org.depsea.log.config.properties.DingtalkProperties;
import org.depsea.log.config.properties.LinkConfig;
import org.depsea.log.config.properties.NotificationProperties;
import org.depsea.log.notification.DingtalkExceptionNotification;
import org.depsea.log.notification.link.Link;
import org.depsea.log.notification.link.LinkFactory;
import org.depsea.log.notification.matcher.DefaultIgnoreExceptionMatcher;
import org.depsea.log.notification.matcher.IgnoreExceptionMatcher;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Wang Chengwei
 * @since 1.0.0 [2022/12/3 17:33]
 */
@EnableConfigurationProperties({
        NotificationProperties.class,
        DingtalkProperties.class
})
@ConditionalOnProperty(prefix = "spring.request-log.notification", name = "enabled", havingValue = "true")
@AutoConfigureAfter(RequestAopLogAutoConfiguration.class)
public class NotificationAutoConfiguration {

    private final DingtalkProperties dingtalkProperties;

    private final NotificationProperties notificationProperties;

    public NotificationAutoConfiguration(DingtalkProperties dingtalkProperties, NotificationProperties notificationProperties) {
        this.dingtalkProperties = dingtalkProperties;
        this.notificationProperties = notificationProperties;
    }

    @Bean
    @ConditionalOnMissingBean(DingTalkClient.class)
    @ConditionalOnProperty(prefix = "spring.request-log.notification.dingtalk", name = "enabled", havingValue = "true")
    public DingTalkClient dingTalkClient() {
        return new DefaultDingTalkClient(this.dingtalkProperties.getWebHookUrl());
    }

    @Bean
    @ConditionalOnProperty(prefix = "spring.request-log.notification.dingtalk", name = "enabled", havingValue = "true")
    public DingtalkExceptionNotification dingtalkExceptionNotification(
            DingTalkClient dingTalkClient, IgnoreExceptionMatcher ignoreExceptionMatcher) {
        List<Link> links = new ArrayList<>();
        if (!CollectionUtils.isEmpty(this.dingtalkProperties.getLinkConfigs())) {
            for (LinkConfig linkConfig : this.dingtalkProperties.getLinkConfigs()) {
                links.add(LinkFactory.createLink(linkConfig));
            }
        }
        DingtalkExceptionNotification exceptionNotification = new DingtalkExceptionNotification(dingTalkClient, links);
        exceptionNotification.setIgnoreExceptionMatcher(ignoreExceptionMatcher);
        return exceptionNotification;
    }

    @Bean
    @ConditionalOnMissingBean(IgnoreExceptionMatcher.class)
    public IgnoreExceptionMatcher ignoreExceptionMatcher() {
        return new DefaultIgnoreExceptionMatcher(this.notificationProperties.getIgnoreExceptions());
    }
}
